﻿package com.oxylusflash.AccordionMenu
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import fl.data.DataProvider;
	import fl.controls.TileList;
	import fl.controls.ScrollBarDirection;
	import caurina.transitions.Tweener;
	import caurina.transitions.properties.ColorShortcuts;
	ColorShortcuts.init();
	
	
	public class SubMenuItem extends Sprite
	{
		
		public var outState:Sprite;
		public var overState:Sprite;
		public var Text:TextField;
		private var link:String;
		private var target:String;
		public var BgTileList:TileList;
	
		
		//constructor
		public function SubMenuItem()
		{
			this.buttonMode = true;
			Text.mouseEnabled = false;
			overState.alpha = 0;
			this.addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler);
			this.addEventListener(MouseEvent.ROLL_OUT, onRollOutHandler);
			this.addEventListener(MouseEvent.CLICK, onClickHandler);
			Text.autoSize = "left";
		}
		
		
		//handler for the roll over event
		private function onRollOverHandler(e:MouseEvent):void 
		{
			Tweener.addTween(overState, { alpha:1, time: 0.3, transition: "easeoutquad" });
			Tweener.addTween(Text, { _color:0x54555f, time: 0.3, transition: "easeoutquad" });
		}
		
		//handler for the roll out event
		private function onRollOutHandler(e:MouseEvent):void 
		{
			Tweener.addTween(overState, { alpha:0, time: 0.3, transition: "easeoutquad" } );
			Tweener.addTween(Text, { _color:0x2A2B2B, time: 0.3, transition: "easeoutquad" });
		}
		
		//set the thumbnail and text
		public function setImageAndText(a:XML, padding:Number, tw:Number, th:Number, menuItemWidth:Number, menuItemHeight:Number, bgDp:DataProvider, titleText:String):void {
			if (a.thumbnail.text() > "") {
				var smallThumb:SmallThumb = SmallThumb(addChild(new SmallThumb()));
				smallThumb.x = padding;
				smallThumb.y = padding;
				smallThumb.setImage(a.thumbnail.text(), tw, th);
				
				Text.htmlText = titleText;//a.label.text();
				Text.x = smallThumb.x + smallThumb.width + 6;
				Text.y = 6;
				outState.width = menuItemWidth - 4;
				overState.width = outState.width;
				Text.width = outState.width - smallThumb.width - 6;
				if ((Math.round(Text.height) + 9) > outState.height) {
					smallThumb.y = Text.y + 5;
				}
				//outState.height = Math.round(Text.height) + 9;
				outState.height = menuItemHeight;
				overState.height = outState.height;
			} else {
				Text.x = 6;
				Text.y = 6;
				Text.htmlText = titleText;//a.label.text();
				//Text.text = "Test";
				outState.width = menuItemWidth - 4;
				overState.width = outState.width;
				Text.width = outState.width - 6;
				//outState.height = Math.round(Text.height) + 9;
				//outState.height = menuItemHeight;
				outState.height = 900;
				overState.height = outState.height;
				/*if(bgDp != null) {
					this.BgTileList.dataProvider = bgDp;
					this.BgTileList.direction = ScrollBarDirection.VERTICAL;
					this.BgTileList.rowHeight = 50;
					this.BgTileList.columnCount =2;
					this.BgTileList.width = outState.width-10;
    			}*/
				//this.addChild(TileListBg);
				//this.addChild(ShapeTileList);
			}
			link = a.link.text();
			target = a.target.text() || "_blank";
		}
		
		//click handler
		private function onClickHandler(e:MouseEvent):void {
			navigateToURL(new URLRequest(link), target);
		}
		
		//get&set height
		override public function set height(value:Number):void { }
		override public function get height():Number { 
			return outState.height;
		}
	}
}